﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YJKTextDimension.Commands;
using YJKTextDimension.Utility;
using YJKTextTool;

namespace YJKTextDimension.Forms
{
	public partial class DlgLeaderDim : System.Windows.Forms.Form
	{
		public DlgLeaderDim()
		{
			this.InitializeComponent();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void LeadToLeaderForm_Load(object sender, EventArgs e)
		{
			this.ckbShareLeader.Checked = true;
			this.cboLineAngle.Enabled = false;
			Helper.AddCombItems(DimDate.FontFamily, this.cboFontType);
			Helper.AddCombItems(DimDate.ArrowType, this.cboArrowStyle);
			if (this.cboAlignStyle.Items.Count > 0)
			{
				this.cboAlignStyle.SelectedIndex = 0;
			}
			if (this.cboLineAngle.Items.Count > 0)
			{
				this.cboLineAngle.SelectedIndex = 0;
			}
			if (this.cboFontType.Items.Count > 0)
			{
				this.cboFontType.SelectedIndex = 0;
			}
			this.ReadDlgData();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelCommand()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgLeaderDim.keybd_event(bVk, 0, dwFlags, 0);
			DlgLeaderDim.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void DlgLeaderDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			int wMsg = 7;
			DlgLeaderDim.SendMessage(CmdLeaderDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			this.SaveDlgData();
			this.CancelCommand();
		}

		private void DlgLeaderDim_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void SaveDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "DlgLeaderDim.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = this.ckbLinupMoreText.Checked ? "0" : "1";
			streamWriter.WriteLine(value);
			value = (this.ckbLineDownMoreText.Checked ? "0" : "1");
			streamWriter.WriteLine(value);
			List<string> list = SpecialCharactorDrawingKit.SplitString(this.txtLineUp.Text);
			streamWriter.WriteLine(list.Count);
			foreach (string value2 in list)
			{
				streamWriter.WriteLine(value2);
			}
			List<string> list2 = SpecialCharactorDrawingKit.SplitString(this.txtLineDown.Text);
			streamWriter.WriteLine(list2.Count);
			foreach (string value3 in list2)
			{
				streamWriter.WriteLine(value3);
			}
			streamWriter.WriteLine(this.cboFontType.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboArrowStyle.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cboAlignStyle.SelectedIndex.ToString());
			value = (this.ckbFixedAngle.Checked ? "0" : "1");
			streamWriter.WriteLine(value);
			value = (this.ckbShareLeader.Checked ? "0" : "1");
			streamWriter.WriteLine(value);
			value = (this.ckbMoreLeader.Checked ? "0" : "1");
			streamWriter.WriteLine(value);
			streamWriter.WriteLine(this.cboLineAngle.SelectedIndex.ToString());
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadDlgData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "DlgLeaderDim.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string value = streamReader.ReadLine();
			if (Convert.ToInt32(value) == 0)
			{
				this.ckbLinupMoreText.Checked = true;
			}
			else
			{
				this.ckbLinupMoreText.Checked = false;
			}
			value = streamReader.ReadLine();
			if (Convert.ToInt32(value) == 0)
			{
				this.ckbLineDownMoreText.Checked = true;
			}
			else
			{
				this.ckbLineDownMoreText.Checked = false;
			}
			int num = Convert.ToInt32(streamReader.ReadLine());
			List<string> list = new List<string>();
			for (int i = 0; i < num; i++)
			{
				list.Add(streamReader.ReadLine());
			}
			this.txtLineUp.Text = SpecialCharactorDrawingKit.MergeString(list);
			int num2 = Convert.ToInt32(streamReader.ReadLine());
			List<string> list2 = new List<string>();
			for (int i = 0; i < num2; i++)
			{
				list2.Add(streamReader.ReadLine());
			}
			this.txtLineDown.Text = SpecialCharactorDrawingKit.MergeString(list2);
			value = streamReader.ReadLine();
			int num3 = Convert.ToInt32(value);
			if (this.cboFontType.Items.Count > num3)
			{
				this.cboFontType.SelectedIndex = num3;
			}
			value = streamReader.ReadLine();
			num3 = Convert.ToInt32(value);
			if (this.cboArrowStyle.Items.Count > num3)
			{
				this.cboArrowStyle.SelectedIndex = num3;
			}
			value = streamReader.ReadLine();
			num3 = Convert.ToInt32(value);
			if (this.cboAlignStyle.Items.Count > num3)
			{
				this.cboAlignStyle.SelectedIndex = num3;
			}
			value = streamReader.ReadLine();
			if (Convert.ToInt32(value) == 0)
			{
				this.ckbFixedAngle.Checked = true;
			}
			else
			{
				this.ckbFixedAngle.Checked = false;
			}
			value = streamReader.ReadLine();
			if (Convert.ToInt32(value) == 0)
			{
				this.ckbShareLeader.Checked = true;
			}
			else
			{
				this.ckbShareLeader.Checked = false;
			}
			value = streamReader.ReadLine();
			if (Convert.ToInt32(value) == 0)
			{
				this.ckbMoreLeader.Checked = true;
			}
			else
			{
				this.ckbMoreLeader.Checked = false;
			}
			value = streamReader.ReadLine();
			num3 = Convert.ToInt32(value);
			if (this.cboLineAngle.Items.Count > num3)
			{
				this.cboLineAngle.SelectedIndex = num3;
			}
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void LeadToLeaderForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgLeaderDim.SendMessage(CmdLeaderDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				base.Focus();
			}
			try
			{
				this.alignStyle = this.cboAlignStyle.SelectedItem.ToString();
				this.LineUpText = this.txtLineUp.Text.Trim();
				this.LineDownText = this.txtLineDown.Text.Trim();
				this.fontType = this.cboFontType.SelectedItem.ToString();
				this.arrowType = this.cboArrowStyle.SelectedItem.ToString();
				this.lineUpRows = this.txtLineUp.Lines.Length;
				this.lineDownRows = this.txtLineDown.Lines.Length;
				if (this.txtLineUp.Text.Length > 0)
				{
					this.LineUpmaxLenth = Helper.ReadTextMaxLenth(this.txtLineUp);
				}
				if (this.txtLineDown.Text.Length > 0)
				{
					this.LineDownmaxLent = Helper.ReadTextMaxLenth(this.txtLineDown);
				}
				this.ckbFixAngle = this.ckbFixedAngle.Checked;
				this.ckbfixAngleSelect = this.cboLineAngle.SelectedItem.ToString();
			}
			catch (Exception)
			{
				this.ckbfixAngleSelect = this.cboLineAngle.Text;
			}
		}

		private void ckbShareLeader_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbShareLeader.Checked)
			{
				this.isCheckedShareLeader = true;
				this.ckbMoreLeader.Enabled = false;
				return;
			}
			this.isCheckedShareLeader = false;
			this.ckbMoreLeader.Enabled = true;
		}

		private void ckbMoreLeader_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbMoreLeader.Checked)
			{
				this.isMorePointShareLine = true;
				this.ckbShareLeader.Enabled = false;
				return;
			}
			this.ckbShareLeader.Enabled = true;
			this.isMorePointShareLine = false;
		}

		private void cboArrowStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.arrowType = this.cboArrowStyle.SelectedItem.ToString();
			base.Focus();
		}

		private void ckbLinupMoreText_CheckedChanged(object sender, EventArgs e)
		{
			int height = this.txtLineUp.Height;
			if (this.ckbLinupMoreText.Checked)
			{
				this.txtLineUp.Multiline = true;
				this.txtLineUp.ScrollBars = ScrollBars.Vertical;
				this.txtLineUp.Height = height * 2;
				int y = this.label2.Location.Y;
				int y2 = this.txtLineDown.Location.Y;
				int y3 = this.ckbLineDownMoreText.Location.Y;
				base.Height += height;
				this.label2.Location = new System.Drawing.Point(this.label2.Location.X, y + height);
				this.txtLineDown.Location = new System.Drawing.Point(this.txtLineDown.Location.X, y2 + height);
				this.ckbLineDownMoreText.Location = new System.Drawing.Point(this.ckbLineDownMoreText.Location.X, y3 + height);
				return;
			}
			this.txtLineUp.Multiline = false;
			this.txtLineUp.ScrollBars = ScrollBars.None;
			int y4 = this.label2.Location.Y;
			int y5 = this.txtLineDown.Location.Y;
			int y6 = this.ckbLineDownMoreText.Location.Y;
			base.Height -= height / 2;
			List<string> list = SpecialCharactorDrawingKit.SplitString(this.txtLineUp.Text);
			if (list.Count > 0)
			{
				this.txtLineUp.Text = list[0];
			}
			this.label2.Location = new System.Drawing.Point(this.label2.Location.X, y4 - height / 2);
			this.txtLineDown.Location = new System.Drawing.Point(this.txtLineDown.Location.X, y5 - height / 2);
			this.ckbLineDownMoreText.Location = new System.Drawing.Point(this.ckbLineDownMoreText.Location.X, y6 - height / 2);
		}

		private void ckbLineDownMoreText_CheckedChanged(object sender, EventArgs e)
		{
			int y = this.label2.Location.Y;
			int y2 = this.label1.Location.Y;
			int height = this.txtLineDown.Height;
			if (this.ckbLineDownMoreText.Checked)
			{
				int y3 = this.label2.Location.Y;
				int y4 = this.txtLineDown.Location.Y;
				int y5 = this.ckbLineDownMoreText.Location.Y;
				this.txtLineDown.Multiline = true;
				this.txtLineDown.ScrollBars = ScrollBars.Vertical;
				this.txtLineDown.Height = height * 2;
				base.Height += height;
				this.label2.Location = new System.Drawing.Point(this.label2.Location.X, y3);
				this.ckbLineDownMoreText.Location = new System.Drawing.Point(this.ckbLineDownMoreText.Location.X, y5);
				this.txtLineDown.Location = new System.Drawing.Point(this.txtLineDown.Location.X, y5);
				return;
			}
			int y6 = this.label2.Location.Y;
			int y7 = this.txtLineDown.Location.Y;
			int y8 = this.ckbLineDownMoreText.Location.Y;
			this.txtLineDown.Multiline = false;
			this.txtLineDown.ScrollBars = ScrollBars.None;
			base.Height -= height / 2;
			this.label2.Location = new System.Drawing.Point(this.label2.Location.X, y6);
			this.ckbLineDownMoreText.Location = new System.Drawing.Point(this.ckbLineDownMoreText.Location.X, y8);
			this.txtLineDown.Location = new System.Drawing.Point(this.txtLineDown.Location.X, y8);
			List<string> list = SpecialCharactorDrawingKit.SplitString(this.txtLineDown.Text);
			if (list.Count > 0)
			{
				this.txtLineDown.Text = list[0];
			}
		}

		private void ckbFixedAngle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbFixedAngle.Checked)
			{
				this.cboLineAngle.Enabled = true;
				this.ckbFixAngle = true;
				return;
			}
			this.cboLineAngle.Enabled = false;
			this.ckbFixAngle = false;
		}

		private void cboLineAngle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.ckbfixAngleSelect = "0";
			try
			{
				this.ckbfixAngleSelect = this.cboLineAngle.SelectedItem.ToString();
			}
			catch
			{
				this.cboLineAngle.SelectedItem = "0";
			}
			base.Focus();
		}

		private void cboFontType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboFontType.Items.Count > 0)
			{
				this.fontType = this.cboFontType.SelectedItem.ToString();
			}
			base.Focus();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("°");
		}

		private void cboLineAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			Helper.TxtSetNum(sender, e);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("±");
		}

		private void button3_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("φ");
		}

		private void button4_Click(object sender, EventArgs e)
		{
			this.AddTextToTextBox("%");
		}

		private void txtLineUp_MouseClick(object sender, MouseEventArgs e)
		{
			this.checktxt = true;
		}

		private void txtLineDown_MouseClick(object sender, MouseEventArgs e)
		{
			this.checktxt = false;
		}

		private void cboAlignStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cboAlignStyle.Items.Count > 0)
			{
				this.alignStyle = this.cboAlignStyle.SelectedItem.ToString();
			}
		}

		private void txtLineUp_TextChanged(object sender, EventArgs e)
		{
			this.LineUpText = this.txtLineUp.Text;
			if (this.txtLineUp.Text.Length > 0)
			{
				this.LineUpmaxLenth = Helper.ReadTextMaxLenth(this.txtLineUp);
			}
		}

		private void txtLineDown_TextChanged(object sender, EventArgs e)
		{
			this.LineDownText = this.txtLineDown.Text;
			if (this.txtLineDown.Text.Length > 0)
			{
				this.LineDownmaxLent = Helper.ReadTextMaxLenth(this.txtLineDown);
			}
		}

		private void button9_Click(object sender, EventArgs e)
		{
			DlgDictForm dlgDictForm = new DlgDictForm();
			if (dlgDictForm.ShowDialog(this) == DialogResult.OK)
			{
				if (this.checktxt)
				{
					TextBox textBox = this.txtLineUp;
					textBox.Text += dlgDictForm.text;
				}
				else
				{
					TextBox textBox2 = this.txtLineDown;
					textBox2.Text += dlgDictForm.text;
				}
			}
			this.SetFocusWithTextBox();
		}

		private void button5_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC00");
			this.AddTextToTextBox(str);
		}

		private void button6_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC01");
			this.AddTextToTextBox(str);
		}

		private void button7_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC02");
			this.AddTextToTextBox(str);
		}

		private void button8_Click(object sender, EventArgs e)
		{
			string str = WindowsCharacterSet.UnicodeToString("AC03");
			this.AddTextToTextBox(str);
		}

		private void AddTextToTextBox(string str)
		{
			TextBox textBox;
			if (this.checktxt)
			{
				textBox = this.txtLineUp;
			}
			else
			{
				textBox = this.txtLineDown;
			}
			int selectionStart = textBox.SelectionStart;
			if (selectionStart >= 0)
			{
				textBox.Text = textBox.Text.Insert(selectionStart, str);
				textBox.Focus();
				textBox.SelectionStart = selectionStart + str.Length;
				return;
			}
			TextBox textBox2 = textBox;
			textBox2.Text += str;
			textBox.Focus();
			textBox.SelectionStart = selectionStart + str.Length;
		}

		private void SetFocusWithTextBox()
		{
			TextBox textBox;
			if (this.checktxt)
			{
				textBox = this.txtLineUp;
			}
			else
			{
				textBox = this.txtLineDown;
			}
			textBox.Focus();
			textBox.SelectionStart = textBox.Text.Length;
		}

		public string arrowType;

		public string fontType;

		public string alignStyle;

		public string LineUpText;

		public string LineDownText;

		public bool isCheckedShareLeader = true;

		public string LineUpmaxLenth;

		public string LineDownmaxLent;

		public string ckbfixAngleSelect;

		public bool ckbFixAngle;

		public bool isMorePointShareLine;

		public int lineUpRows = 1;

		public int lineDownRows = 1;

		public bool checktxt = true;
	}
}
